//
//  CERuleView.m
//  digitalCurrency
//
//  Created by 来迎宾 on 2019/4/4.
//  Copyright © 2019年 XinHuoKeJi. All rights reserved.
//

#import "CERuleView.h"

@implementation CERuleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createviews];
    }
    return self;
}

- (void)createviews {
    NSString *STRING = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@\n%@",
                      LocalizationKey(@"No.1 will solely win the first bonus"),
                      LocalizationKey(@"No.2-3 will share the second bonus"),
                      LocalizationKey(@"No. 4-7 will share the third bonus"),
                      LocalizationKey(@"No. 8-15 will share the fourth bonus"),
                      LocalizationKey(@"No. 16-31 will share the fifth bonus"),
                      LocalizationKey(@"No. 32-61 will share the sixth bonus"),
                      LocalizationKey(@"No. 62-127 will share the seventh bonus"),
                      LocalizationKey(@"No. 128-255 will share the eighth bonus"),
                      LocalizationKey(@"No. 256-511 will share the ninth bonus")];

    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = [UIColor colorWithHEX:@"C0C2C6"];
    bgview.layer.cornerRadius = 5;
    [self addSubview:bgview];
    [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(SCREEN_WIDTH-30);
        make.height.mas_equalTo(345);
    }];
    
    
    NSMutableAttributedString *attributedStr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",LocalizationKey(@"Distribute ranking"),LocalizationKey(@"(No. 1 - No. 511)")]];
    NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle1.lineSpacing = 8.0; // 设置行间距
    paragraphStyle1.alignment = NSTextAlignmentJustified; //设置两端对齐显示
    [attributedStr1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, attributedStr1.length)];
    
    UILabel *titletips = [[UILabel alloc] init];
    titletips.attributedText = attributedStr1;
    titletips.numberOfLines = 0;
    titletips.textAlignment = NSTextAlignmentCenter;
    titletips.font = [UIFont systemFontOfSize:15];
    titletips.textColor = [UIColor colorWithHEX:maincolor];
    titletips.backgroundColor = [UIColor clearColor];
    [bgview addSubview:titletips];
    
    [titletips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(SCREEN_WIDTH-60);
        make.height.mas_equalTo(60);
    }];
    
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:STRING];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8.0; // 设置行间距
    paragraphStyle.alignment = NSTextAlignmentJustified; //设置两端对齐显示
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedStr.length)];
    
    UILabel *rulelabel = [[UILabel alloc] init];
    rulelabel.numberOfLines = 0;
    //rulelabel.textAlignment = NSTextAlignmentCenter;
    rulelabel.font = [UIFont systemFontOfSize:13];
    rulelabel.textColor = [UIColor colorWithHEX:mainBlackColor];
    rulelabel.backgroundColor = [UIColor clearColor];
    rulelabel.attributedText = attributedStr;
    [bgview addSubview:rulelabel];
    self.rulelabel = rulelabel;
    [rulelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(85);
        make.centerX.mas_equalTo(0);
        make.width.lessThanOrEqualTo(SCREEN_WIDTH-50);
        make.bottom.mas_equalTo(-28);
    }];
    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//    //[btn setTitle:@"合起" forState:UIControlStateNormal];
//    [self addSubview:btn];
//    self.flipbtn = btn;
//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(280);
//        make.left.mas_equalTo(15);
//        make.width.mas_equalTo(SCREEN_WIDTH-30);
//        make.height.mas_equalTo(20);
//    }];
}

    /** 计算cell高度 */
- (CGFloat)heightWithText:(NSString *)text {
    NSString *string = text;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange allRange = [string rangeOfString:string];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13.0] range:allRange];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:allRange];
    
    CGFloat titleHeight;
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    // 获取label的最大宽度
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-60, CGFLOAT_MAX)options:options context:nil];
    titleHeight = ceilf(rect.size.height);
    
    return titleHeight; // 动态高度 + 静态高度
}
@end
